<?php
session_start();
class login extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       private $_configs = null;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('javascript');
            $this->load->library('session');
            $this->load->library('smarty3','','smarty');
            $this->_configs["title"]="Account Management";
            $this->_configs["authority"]="account";
            $this->_configs["MinAuthority"]=1;
            $this->load->model('admin-planners/account_model','account_model');
        }
        public function index()
	{
            $this->smarty->display('admin-planners/login/01_login');
	}
        public function checkauthority(){
            if(isset($_SESSION["ADP-USER"]["AUTHORITY"])){
                $myau=-1;
                if(is_array($_SESSION["ADP-USER"]["AUTHORITY"])){
                    foreach ($_SESSION["ADP-USER"]["AUTHORITY"] as $au){
                        if($au["keyword"]=="view" && $myau==-1)$myau=0;
                        if($au["keyword"]=="admin")$myau=1;
                        if($au["keyword"]==$this->_configs["authority"]){
                            if($au["value"]==0 && $myau==-1) $myau=0;
                            if($au["value"]==1) $myau=1;
                        }
                    }
                }
                return $myau>=$this->_configs["MinAuthority"]?$myau:-1;
            }return -1;
        }
        function Group_Authority($name){
            $auth_list=$this->account_model->getgroup_au($name);
            $configs["auth_list"]=$auth_list;
            $this->smarty->assign('configs', $configs);
            $this->smarty->display('admin-planners/account/03_group_auth');
        }
        
        function signin(){
            
            $account=$this->account_model->login($_POST["username"],md5($_POST["password"]));
            if(count($account)>0){
                $_SESSION["ADP-USER"]=objectToArray($account[0]);
                $authority=$_SESSION["ADP-USER"]["authority"].$_SESSION["ADP-USER"]["groupauthority"];
                $authority_ls=$this->account_model->getAuthoritys($authority);
                $_SESSION["ADP-USER"]["AUTHORITY"]=objectToArray($authority_ls);
                //JO_location();
                $result=array(
                "code"      =>  0,
                "msg"       => "Login successful."
                );
            }else{
                $result=array(
                "code"      =>  -1,
                "msg"       => "Login fail.Please, check Username or Password <br/>"
                );
            }
            echo json_encode($result);
        }
        function logout(){
            unset($_SESSION["ADP-USER"]);
            unset($_SESSION["ADP-USER"]["AUTHORITY"]);
            unset($_SESSION["MYAUTHORITY"]);
            //JO_location();
            //redirect(base_url()."admin-planners/");
            $result=array(
            "code"      =>  0,
            "msg"       => "Logout successful."
            );
            echo json_encode($result);
        }
        
       
        
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */